package com.itheima.test22;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class copyMethod implements Runnable {
    private Socket socket;

    public copyMethod(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            UUID uuid = UUID.randomUUID();
            String s = uuid.toString();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("TEST10.16", s + ".jpg")));
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, bytes.length);
                bos.flush();
            }
            bos.close();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("文件上传成功");
            bw.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
